/* 一个模块 和 一个模块 的vuex 一起操作 */

import { Module } from 'vuex';

import { IRootState } from '@/store/types';
import { ISystemState } from './types';
import {
  getPageListServer,
  deletePageItemServer,
  createPageDataServer,
  editPageDataServer
} from '@/service/main/system/system';

const systemModule: Module<ISystemState, IRootState> = {
  namespaced: true,
  state() {
    return {
      usersList: [],
      usersCount: 0,
      roleList: [],
      roleCount: 0,
      goodsList: [],
      goodsCount: 0,
      menuList: [],
      menuCount: 0
    };
  },
  mutations: {
    changeUsersList(state, usersList: any[]) {
      state.usersList = usersList;
    },
    changeUsersCount(state, usersCount: number) {
      state.usersCount = usersCount;
    },
    changeRoleList(state, list: any[]) {
      state.roleList = list;
    },
    changeRoleCount(state, count: number) {
      state.roleCount = count;
    },
    changeGoodsList(state, list: any[]) {
      state.goodsList = list;
    },
    changeGoodsCount(state, count: number) {
      state.goodsCount = count;
    },
    changeMenuList(state, list: any[]) {
      state.menuList = list;
    },
    changeMenuCount(state, count: number) {
      state.menuCount = count;
    }
  },
  getters: {
    // 统一处理一种类型的数据
    pageListData(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}List`];
      };
    },
    pageCount(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}Count`];
      };
    }
  },
  actions: {
    // 这是一个公用的方法，所以叫pageList 因为这些请求list的方式都是相同的
    async getPageListAction({ commit }, payload: any) {
      const pageUrl = `/${payload.pageName}/list`;
      // 1.对页面发送请求
      const result = await getPageListServer(pageUrl, payload.pageInfo);
      const { list, totalCount } = result.data;
      commit(
        `change${
          payload.pageName[0].toUpperCase() + payload.pageName.substr(1)
        }List`,
        list
      );
      commit(
        `change${
          payload.pageName[0].toUpperCase() + payload.pageName.substr(1)
        }Count`,
        totalCount
      );
    },

    // 删除列表中的一个内容
    async deletePageItemAction({ dispatch }, payload: any) {
      // 1. 拼接删除请求路径
      const { pageName, id } = payload;
      const pageUrl = `/${pageName}/${id}`;

      // 2. 放松网络请求
      await deletePageItemServer(pageUrl);

      // 3.重新请求数据
      dispatch('getPageListAction', {
        pageName,
        pageInfo: { size: 10, offset: 0 }
      });
    },

    // 添加
    async createPageDataAction({ dispatch }, payload: any) {
      const { pageName, newData } = payload;
      const pageUrl = `/${pageName}`;
      await createPageDataServer(pageUrl, newData);
      dispatch('getPageListAction', {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      });
    },
    // 编辑
    async editPageDataAction({ dispatch }, payload: any) {
      const { pageName, editData, id } = payload;
      const pageUrl = `/${pageName}/${id}`;
      await editPageDataServer(pageUrl, editData);
      dispatch('getPageListAction', {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      });
    }
  }
};

export default systemModule;
